<html>
<head>
<title>Quick Tag Editor</title>
<script type="text/javascript" src="../tag-lib.js"></script>
<script type="text/javascript" src="../../../popups/popup.js"></script>
<script type="text/javascript">

window.resizeTo(400, 180);
var QuickTag = window.opener.QuickTag;
var curTag = 'none';
var curTagOpt = false;
var CSSEdit = false;
var editor = null;

function Init() {
  __dlg_translate("QuickTag");
  __dlg_init();

  var param = window.dialogArguments;
  editor = param['editor'];
  var selectext = param['selectedText']; // sem uso...

  createDropdown("tags");

  var to = document.getElementById('tagopen');
  if (document.all) 
    to.attachEvent("onkeypress", function(e) { choice_dropdown(e) });
  else 
    to.addEventListener("keypress", function(e) { choice_dropdown(e) }, true);
  to.focus();

  document.getElementById('bt_colors').style.display = 'none';
  document.body.onkeypress = __dlg_key_press;
}

function onCancel() {
  __dlg_close(null);
  return false;
}

function quoteTest(val) {
  var er = /^\w+\s*(\w+=\"[^\"]*\"\s*|\w+=\'[^\']*\'\s*)*$/;
  return er.test(val);
}

function onOK() {
  var el = document.getElementById('tagopen');
  if(!el.value) {
  alert(HTMLArea._lc("Enter the TAG you want to insert", "QuickTag"));
  el.focus();
  return false;
  }
  if(!quoteTest(el.value)) {
    alert(HTMLArea._lc("There are some unclosed quote", "QuickTag"));
    el.focus();
    el.select();
    return false;
  }
  var param = {};
  var cleanTO = document.getElementById('tagopen').value.replace(/(<|>)/g,"");
  param.tagopen = "<"+cleanTO+">";
  param.tagclose = param.tagopen.replace(/^<(\w+) ?.*>/,"</$1>");

  var subtag = subTagLib[curTag];
  if(typeof subtag == 'object') {
    param.tagopen = param.tagopen+subtag.op;
    param.tagclose = subtag.cl+param.tagclose;
  }

  __dlg_close(param);
  return false;
}

function __dlg_key_press(ev) {
  ev || (ev = window.event);
  switch(ev.keyCode) {
    case 13:
    document.getElementById('bt_ok').click();
      break;
    case 27:
    __dlg_close();
    return false;
  }
  return true;
}

function selchange() {
  var toadd = document.getElementById('selectag').value;
  var oldval = document.getElementById('tagopen').value;
  var text = (oldval+toadd).replace(/^\s*(.+)\s*$/,"$1");
  var atrib = /(\w+)=\"$/.test(toadd) ? toadd.replace(/(\w+)=\"$/, "$1") : null;
  var showcolors = (/color\: ?$/.test(toadd)) ? true : false;
  var noCSSclasses = false;
  var obj = null;
  curTag = text.replace(/(^\w+) ?.*$/, "$1");
  curTagOpt = (tagLib[curTag] == true);

  if(atrib) {
    var atrer = eval('/'+atrib+'=/ig');
    if(atrer.test(oldval) && !(/[^=]\"$/.test(toadd))) {
      alert(HTMLArea._lc("This attribute already exists in the TAG", "QuickTag"));
      return false;
    }

    switch(atrib) {
    case 'style':
      CSSEdit = true;
      break;
    case 'class':
      if(captureClasses() == 0) noCSSclasses = true;
      break;
    case 'color': case 'bgcolor': case 'bordercolor':
    case 'bordercolorlight': case 'bordercolordark':
      showcolors = true;
      break;
    }

    if(curTagOpt == true)
      obj = eval('opAtt_'+curTag+'["'+atrib+'"]');
    else
      obj = opAtt_all[atrib];
  }
  else if(!CSSEdit && (/(^\w+$|\"$)/.test(toadd))) {
    obj = 'all';
    if(curTagOpt == true) obj = curTag;
    toadd += ' ';
  }

  if(CSSEdit && quoteTest(text))
    CSSEdit = false;

  if(showcolors)
    document.getElementById('bt_colors').style.display = '';

  if(obj)
    createDropdown(obj);
  else if(!CSSEdit)
    document.getElementById('showselect').style.visibility = 'hidden';

  if(noCSSclasses) {
    document.getElementById('showselect').innerHTML = HTMLArea._lc("No CSS class avaiable", "QuickTag");
    document.getElementById('showselect').style.visibility = 'visible';
  }

  addchanges();

  function addchanges() {
    document.getElementById('tagopen').focus();
    document.getElementById('tagopen').value += toadd;
  }
}

function captureClasses() {
  var cont = 0;
  // If there is no '<HTML>' in the HTMLArea content, when using
  // styleSheets[..].rule, IE generates an error.
  // In this case, we lost the classes in external CSS files.
  // If FullPage is loaded, there is no problem.
  if(!(/<\s*html\s*>/i.test(editor.getHTML())) && document.all) {
    var styles = editor._doc.getElementsByTagName("style");
    for(var i in styles) {
      var cont = styles[i].innerHTML;
      if(typeof cont != 'undefined') {
        var cls = cont.match(/\.\w+/ig,"");
        for(j in cls) if(/^\.\w+$/.test(cls[j])) {
          var classname = cls[j].replace(/\./,"")+'"';
          opAtt_all['class'][cls[j]] = classname;
          cont++;
        }
      }
    }
    return cont;
  }

  var styleSheet = null;
  var styles = editor._doc.styleSheets;

  for (var i = 0; i < styles.length; i++) {
    styleSheet = styles[i];
    if(document.all)
      var _rules = styleSheet.rules;
    else
      var _rules = styleSheet.cssRules;
    for (var j = 0; j < _rules.length; j++) {
      rule = _rules[j];
      if(/^\.\w+$/.test(rule.selectorText)) {
        var classname = rule.selectorText.replace(/\./,"")+'"';
        opAtt_all['class'][rule.selectorText] = classname;
        cont++;
      }
    }
  }
  return cont;
}

function choice_dropdown(e) {
  if (document.all) Key = e.keyCode;
  else Key = e.charCode;

  if(Key != 32) return;

  var text = document.getElementById('tagopen').value.replace(/^\s*([^\s]+)\s*$/,"$1");

  if(CSSEdit && quoteTest(text)) CSSEdit = false;
  if(CSSEdit) return;

  if(curTag == 'none') {
    curTag = text.replace(/(^\w+) ?.*$/, "$1");
    curTagOpt = tagLib[curTag];
  }

  if(!/\w/.test(text)) {
    document.getElementById('tagopen').value = '';
    createDropdown("tags");
    return;
  }

  var att = 'all';
  if(curTagOpt == true)
    att = curTag;

  createDropdown(att);
}

function createDropdown(type) {
  var _div = document.getElementById('showselect');
  while(_div.hasChildNodes())
    _div.removeChild(_div.firstChild);
  _div.style.visibility = 'visible';

  var _sel = document.createElement("select");
  _sel.id = "selectag";
  _sel.onchange = function() { selchange(); };

  if(typeof type == 'object') {
    var obj = type;
    forObj(HTMLArea._lc("OPTIONS", "QuickTag"));
    _div.appendChild(_sel);
    return;
  }

  if(type == 'tags') {
    var obj = allTags;
    forObj(HTMLArea._lc("TAGs", "QuickTag"));
    _div.appendChild(_sel);
    return;
  }

  type = type.replace(/^h[1-6]$/,"h");
  var topt = eval('opTag_'+type);
  if(typeof topt == 'object') 
    var obj = topt;
  else 
    return;

  forObj(HTMLArea._lc("ATTRIBUTES", "QuickTag"));
  _div.appendChild(_sel);

  function forObj(first) {
    if(first) {
      var _op = document.createElement("option");
      _op.appendChild(document.createTextNode(first));
      _sel.appendChild(_op);
    }
    for(i in obj) {
      var opt = document.createElement("option");
      opt.appendChild(document.createTextNode(i));
      opt.value = obj[i];
      _sel.appendChild(opt);
    }
  }
}

function colors() {
  var colloc = window.location.toString().replace(/plugins.+$/,"") +
               "popups/select_color.html";
  window.open(colloc, 'colors', "toolbar=no,location=no,directories=no," +
              "status=no,menubar=no,scrollbars=no,resizable=no,width=240,height=182");
}

// simulation of Dialog._return - select_color.html needs it.
var Dialog = new Object();
Dialog._return = function (val) {
  if(val != null) {
    if(CSSEdit)
      val += '; ';
    else
      val += '"';
    document.getElementById('tagopen').focus();
    document.getElementById('tagopen').value += "#"+val;
    document.getElementById('bt_colors').style.display = 'none';
  }
};
</script>
<style type="text/css">
html, body {
background: ButtonFace;
color: ButtonText;
font: 11px Tahoma,Verdana,sans-serif;
margin: 0px;
padding: 0px;
border-width: 0px;
}
body { padding: 5px; }
table { font: 11px Tahoma,Verdana,sans-serif; }
select, input, button { font: 11px Tahoma,Verdana,sans-serif; }
button { width: 70px; }
.space { padding: 2px; }
.title {
background: #ddf;
color: #000;
font-weight: bold;
font-size: 120%;
padding: 3px 10px;
margin-bottom: 10px;
border-bottom: 1px solid black;
letter-spacing: 2px;
}
.buttons {
border-top: 1px solid #999;
padding: 5px;
text-align: right;
height: 20px;
}
form { padding: 0px; margin: 0px; }
</style>
</head>
  <body class="dialog" onload="Init()">
<form action="" method="get">
  <div class="title" style="width: 360px">Quick Tag Editor</div>
  <div style="text-align: center;">
  <table border="0" width="370">
    <tbody>
      <tr>
        <td colspan="2" style="text-align: center;" style="padding: 3px; background-color: #e9e9e9;">
        <strong style="font-size: 14px">&lt;</strong>
        <input id="tagopen" type="text" style="background-color: #e9e9e9; width: 327px; border-width: 0px;">
        <strong style="font-size: 14px">&gt;</strong>
        </td>
      </tr>
    </tbody>
  </table>
  </div>
<div class="space"></div>
<div class="buttons">
<button type="button" id="bt_colors" onclick="colors();">Colors</button>
<button type="button" id="bt_ok" onclick="return onOK();">Ok</button>
<button type="button" onclick="return onCancel();">Cancel</button>
</div>
<div id="showselect" style="position:absolute; left:7px; top:75px; width:150px; z-index:100; visibility: visible; height: 13px; color: red;">
</div>
</form>
</body>
</html>